﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WhatSNew.AppCode;

namespace WhatSNew.User
{
    public partial class Personal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UserInfo userInfo = (UserInfo)Session["user"];
            if (userInfo == null)
            {
                Response.Redirect("../RegisterAndSign/SignIn.aspx");
            }
            else if (!IsPostBack)
            {
                BTN_Login.Text = userInfo.PInfo.Name + "的账号";
                BTN_Register.Text = "注销";
                TB_Name.Text = userInfo.PInfo.Name;
                TB_Email.Text = userInfo.PInfo.Email;
                //TB_Pwd.Text = userInfo.PInfo.Password;
                TB_Intro.Text = userInfo.PInfo.Introduction;
                TB_Address.Text = userInfo.PInfo.Address;
                DDL_EmailPriv.ClearSelection();
                DDL_EmailPriv.Items[(int)userInfo.PInfo.PrivOfEmail].Selected = true;
                DDL_BirthdayPriv.ClearSelection();
                DDL_BirthdayPriv.Items[(int)userInfo.PInfo.PrivOfBirthday].Selected = true;
                DDL_AddrPriv.ClearSelection();
                DDL_AddrPriv.Items[(int)userInfo.PInfo.PrivOfAddress].Selected = true;
                DDL_GenderPriv.ClearSelection();
                DDL_GenderPriv.Items[(int)userInfo.PInfo.PrivOfGender].Selected = true;
                DDL_SexOriePriv.ClearSelection();
                DDL_SexOriePriv.Items[(int)userInfo.PInfo.PrivOfSexualOrie].Selected = true;
                switch (userInfo.PInfo.Gender)
                {
                    case GenderType.Male:
                        RB_Male.Checked = true;
                        break;
                    case GenderType.Female:
                        RB_Female.Checked = true;
                        break;
                    default:
                        break;
                }
                switch (userInfo.PInfo.SexualOrie)
                {
                    case GenderType.Male:
                        RB_SMale.Checked = true;
                        break;
                    case GenderType.Female:
                        RB_SFemale.Checked = true;
                        break;
                    default:
                        break;
                }

                string[] str = userInfo.PInfo.Birthday.Split('-');
                TB_Year.Text = str[0];
                TB_Month.Text = str[1];
                TB_Day.Text = str[2];
            }
            LB_OriPwdInfo.Text = "请输入密码";
            LB_OriPwdInfo.ForeColor = System.Drawing.ColorTranslator.FromHtml("#323A3F");
            LB_NewPwdInfo.Text = "请输入新密码";
            LB_NewPwdInfo.ForeColor = System.Drawing.ColorTranslator.FromHtml("#323A3F");
            LB_ReNewPwdInfo.Text = "请确认新密码";
            LB_ReNewPwdInfo.ForeColor = System.Drawing.ColorTranslator.FromHtml("#323A3F");
            LB_RPInfo.Text = "";
            LB_PInfo.Text = "";
        }

        protected void BTN_Update_Click(object sender, EventArgs e)
        {
            UserInfoAccess infoAccess = new UserInfoAccess();
            UserInfo userInfo = (UserInfo)Session["user"];
            if (infoAccess.IsExist(TB_Name.Text) > 0 && userInfo.PInfo.Name != TB_Name.Text)
            {
                LB_PInfo.Text = "[用户名已存在]";
                LB_PInfo.ForeColor = System.Drawing.Color.Red;
            }
            else if (TB_Email.Text.Length != 0 && !REV_Email.IsValid)
            {
                LB_PInfo.Text += "[邮箱格式不对]";
                LB_PInfo.ForeColor = System.Drawing.Color.Red;
                return;
            }
           
            userInfo.PInfo.Name = TB_Name.Text;
            userInfo.PInfo.Email = TB_Email.Text;
            userInfo.PInfo.Introduction = TB_Intro.Text;
            userInfo.PInfo.Address = TB_Address.Text;
            userInfo.PInfo.PrivOfEmail = (PrivacyType)Enum.Parse
                (typeof(PrivacyType), DDL_EmailPriv.SelectedIndex.ToString());
            userInfo.PInfo.PrivOfBirthday = (PrivacyType)Enum.Parse
                (typeof(PrivacyType), DDL_BirthdayPriv.SelectedIndex.ToString());
            userInfo.PInfo.PrivOfGender = (PrivacyType)Enum.Parse
                (typeof(PrivacyType), DDL_GenderPriv.SelectedIndex.ToString());
            userInfo.PInfo.PrivOfSexualOrie = (PrivacyType)Enum.Parse
                (typeof(PrivacyType), DDL_SexOriePriv.SelectedIndex.ToString());
            userInfo.PInfo.PrivOfAddress = (PrivacyType)Enum.Parse
                (typeof(PrivacyType), DDL_AddrPriv.SelectedIndex.ToString());
            if (RB_Male.Checked)
                userInfo.PInfo.Gender = GenderType.Male;
            else if (RB_Female.Checked)
                userInfo.PInfo.Gender = GenderType.Female;

            if (RB_SMale.Checked)
                userInfo.PInfo.SexualOrie = GenderType.Male;
            else if (RB_SFemale.Checked)
                userInfo.PInfo.SexualOrie = GenderType.Female;

            string birthday = TB_Year.Text + "-" + TB_Month.Text + "-" + TB_Day.Text;
            userInfo.PInfo.Birthday = birthday;

            LB_PInfo.Text = "信息修改成功！";
            LB_PInfo.ForeColor = System.Drawing.Color.Green;
            infoAccess.UpDate(userInfo);
            Session["user"] = userInfo;

        }


        protected void BTN_PwdUpdate_Click(object sender, EventArgs e)
        {
            UserInfo userInfo = (UserInfo)Session["user"];
            if (!TB_OriPwd.Text.Equals(userInfo.PInfo.Password))
            {
                LB_OriPwdInfo.Text = "密码错误";
                LB_OriPwdInfo.ForeColor = System.Drawing.Color.Red; 
                return;
            }
            else if (TB_NewPwd.Text.Length < 6)
            {
                LB_NewPwdInfo.Text = "密码须大于6位";
                LB_NewPwdInfo.ForeColor = System.Drawing.Color.Red; 
                return;
            }
            else if (!TB_NewPwd.Text.Equals(TB_NewPwd2.Text))
            {
                Session["pwdLabel"] = LB_RenewPwd;
                LB_ReNewPwdInfo.Text = "两次密码不一致";
                LB_ReNewPwdInfo.ForeColor = System.Drawing.Color.Red; 
                return;
            }
            else
            {
                LB_RPInfo.Text = "密码修改成功！请牢记!";
                LB_RPInfo.ForeColor = System.Drawing.Color.Green;
                return;
            }
        }

        protected void BTN_Register_Click(object sender, EventArgs e)
        {
            Session.Remove("user");
            Response.Redirect("../RegisterAndSign/SignIn.aspx");
        }


    }
}